var NiceBill = function() {
    this._HTMLDom = null;
    this.reserv_type = null;
    this.reserv_id = null;
    this.pay_method = null;
    this.user_id = null;
    this.user_data = null;
    this.pay_data = null;
}

NiceBill.prototype = {
    constructor : NiceBill,
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // InterlockBooking class 에 연동요청
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    /**
     * 결제 데이터 가져오기
     */
    setPayData : function($reserv_id, $reserv_type) {
        var thisClass = this;
        var interlockBooking = new InterlockBooking();
        var data = {
            reserv_id : $reserv_id,
            reserv_type : $reserv_type
        }
        // $.mobile.showPageLoadingMsg();
        interlockBooking.setPayData(data, function($data) {
            // alert("setPayData result :: " + $data);
            thisClass.pay_data = $data;
            thisClass.getProfile(thisClass.user_id);
            // console.log($data);
        });
    },
    /**
     * 유저 데이터 요청
     */
    getProfile : function($userId) {
        var thisClass = this;
        var objJson = new MJson();
        var cm = new ConnectionManager();

        objJson.putClient(MJson.CLIENT.USERID, $userId);

        objJson.putPub(MJson.PUB.SID, "US001");
        objJson.putPub(MJson.PUB.SERVICENAME, "getMyProfile");

        cm.requestGet(objJson.getJsonString(), function($jsonData) {
            // alert("getProfile success :: " + JSON.stringify($jsonData));
            // alert("success check :: " + objJson.checkResult($jsonData));
            // console.log("JSON :: " + JSON.stringify($jsonData));
            if (new MJson().checkServiceResult($jsonData)) {
                var item = $jsonData.msp.contents.items;
                if (item == undefined) {
                    // 데이터가 하나도 없을 경우
                    // alert("해당 지역의 골프장이 없습니다.");
                    return;
                }
                // console.log(item);
                thisClass.user_data = item;

                thisClass.setForm(thisClass._HTMLDom);
            } else {
                console.log("getProfile error");
                console.log($jsonData);
            }
        }, function(e) {
            return;
            alert("fail :: " + JSON.stringify(e));
        });
    },
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Method
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    setForm : function($form) {
        // alert("setForm");
        // TODO
        var form = $form;
        var MallReserved = "q_reserve!@!@" + this.reserv_id + "@!@!mid!@!@" + this.pay_data.mid + "@!@!mkey!@!@" + this.pay_data.mkey;

        form.attr("acceptCharset", "euc-kr");
        form.append(this.getHTMLInput("GoodsName", "sky72 골프인예약"));

        form.append(this.getHTMLInput("Moid", this.reserv_id));

        // TODO TEST 값
        form.append(this.getHTMLInput("MID", "nictest00m"));
        form.append(this.getHTMLInput("Amt", "1000"));
        // form.append(this.getHTMLInput("MID", this.pay_data.mid));
        // form.append(this.getHTMLInput("Amt", this.pay_data.price_pay));
        
        form.append(this.getHTMLInput("ediDate", this.pay_data.edidate));

        form.append(this.getHTMLInput("BuyerName", this.user_data.userName));
        form.append(this.getHTMLInput("BuyerTel", this.user_data.phonenum));
        form.append(this.getHTMLInput("BuyerEmail", this.user_data.email));

        form.append(this.getHTMLInput("ParentEmail", "parent@nicepay.co.kr"));

        // form.append(this.getHTMLInput("UserIP", "www.golfinsns.com"));
        // form.append(this.getHTMLInput("MallIP", "www.golfinsns.com"));

        // form.append(this.getHTMLInput("VbankExpDate", ""));
        form.append(this.getHTMLInput("MallResultFWD", "N"));
        form.append(this.getHTMLInput("EncryptData", ""));
        form.append(this.getHTMLInput("WapUrl", ""));
        form.append(this.getHTMLInput("GoodsCl", ""));
        
        form.append(this.getHTMLInput("PayMethod", this.pay_method));

        form.append(this.getHTMLInput("MallReserved", MallReserved));

        form.append(this.getHTMLInput("RetryURL", "http://www.golfinsns.com/nicepay_billkey_result"));
        form.append(this.getHTMLInput("ReturnUrl", "http://www.golfinsns.com/nicepay_billkey_result"));

        form.append(this.getHTMLInput("CharSet", "euc-kr"));
        form.attr("id", "idPermissionForm");
        
        this.goPay(document.payForm);
    },
    goPay : function(form) {
        alert("sumit!!");
        document.charset = 'euc-kr';
        form.target = "_blank";
        form.method = "post";
        form.action = "https://web.nicepay.co.kr/smart/interfaceURL.jsp";
        form.submit();
    },
    getHTMLInput : function($name, $value) {
        return '<input type="hidden" name="' + $name + '" value="' + $value + '"/>';
    },
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Initialize
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    initialize : function($htmldom) {
        // alert("initialize");
        // alert("nice bill initialize");
        this._HTMLDom = $htmldom;

        this.reserv_type = window.localStorage.getItem("billingreservtype");
        this.reserv_id = window.localStorage.getItem("billingreservid");
        this.pay_method = window.localStorage.getItem("billingmethod");
        this.user_id = window.localStorage.getItem("billinguserid");

        // this.reserv_type = "R";
        // this.reserv_id = "7994749";
        // this.pay_method = "CARD";
        // this.user_id = "kimkm";

        // data clear
        // window.localStorage.removeItem("billingreservtype");
        // window.localStorage.removeItem("billingreservid");
        // window.localStorage.removeItem("billingmethod");
        // window.localStorage.removeItem("billinguserid");

        this.setPayData(this.reserv_id, this.reserv_type);
    }
}

$(document).ready(function() {
    var nice = new NiceBill();
    nice.initialize($("form"));
});
